﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TaskManager
{
    public partial class TabSelect : UserControl
    {
        public TabSelect()
        {
            InitializeComponent();
        }

        //private Color showColor = Color.FromArgb(67, 168, 162);
        private Color showColor = Color.FromArgb(202, 81, 0);
        [Description("字体的显示颜色"), Category("自定义属性")]
        public Color ShowColor
        {
            get
            {
                return showColor;
            }
            set
            {
                showColor = value;
                updateShow();
            }
        }

        private Color selectColor = Color.White;
        [Description("选中时，字体的显示颜色"), Category("自定义属性")]
        public Color SelectColor
        {
            get
            {
                return selectColor;
            }
            set
            {
                selectColor = value;
                updateShow();
            }
        }

        private int seletedIndex = 0;
        [Description("选中的TabSelect索引值"), Category("自定义属性")]
        public int SeletedIndex
        {
            get
            {
                return seletedIndex;
            }
            set
            {
                if (seletedIndex != value)
                {
                    seletedIndex = value;
                    transLabel_Click(null, null);
                }
            }
        }

        public delegate void Click_Handle(object sender, EventArgs e);
        [Description("点击TabSelect上的标签时发生"), Category("自定义事件")]
        public event Click_Handle Click_This;

        private void transLabel_Click(object sender, EventArgs e)
        {
            int preIndex = seletedIndex;

            // 设置选中索引值
            if (sender.Equals(this.transLabel1)) seletedIndex = 0;
            else if (sender.Equals(this.transLabel2)) seletedIndex = 1;
            else if (sender.Equals(this.transLabel3)) seletedIndex = 2;
            else if (sender.Equals(this.transLabel4)) seletedIndex = 3;
            else if (sender.Equals(this.transLabel5)) seletedIndex = 4;
            updateShow();

            // 调用选中索引变动处理逻辑
            if (preIndex != seletedIndex && Click_This != null) Click_This(this, new EventArgs());
        }

        // 控件尺寸变动时，重绘当前控件
        private void TabSelect_SizeChanged(object sender, EventArgs e)
        {
            updateShow();
        }

        // 更新当前控件的显示
        private void updateShow()
        {
            // 设置选中的颜色值
            int i = 0;

            transLabel1.FontColor = (seletedIndex == i++) ? selectColor : showColor;
            transLabel2.FontColor = (seletedIndex == i++) ? selectColor : showColor;
            transLabel3.FontColor = (seletedIndex == i++) ? selectColor : showColor;
            transLabel4.FontColor = (seletedIndex == i++) ? selectColor : showColor;
            transLabel5.FontColor = (seletedIndex == i++) ? selectColor : showColor;

            // 重绘当前控件
            //this.Invalidate();

            // 重绘子控件
            transLabel1.Invalidate();
            transLabel2.Invalidate();
            transLabel3.Invalidate();
            transLabel4.Invalidate();
            transLabel5.Invalidate();
        }

    }
}
